/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

package au.com.lastweekend.jim.swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.Icon;
import javax.swing.JTabbedPane;

/*
 * Created on 21/12/2004 From the Java forums, Thanks Mr Silly!!
 * http://forum.java.sun.com/thread.jspa?threadID=337070&messageID=2865540
 */
/**
 * A JTabbedPane which has a close ('X') icon on each tab.
 * 
 * To add a tab, use the method addTab(String, Component)
 * 
 * To have an extra icon on each tab (e.g. like in JBuilder, showing the file type) use the method addTab(String, Component, Icon).
 * Only clicking the 'X' closes the tab.
 * 
 * @author grant@lastweekend.com.au
 * @version $Id: CloseableJTabbedPane.java,v 1.2 2005/06/09 11:15:27 ggardner Exp $
 */
public class CloseableJTabbedPane extends JTabbedPane implements MouseListener {

    private static final long serialVersionUID = 1L;

    public CloseableJTabbedPane() {

        super();
        addMouseListener(this);
    }

    @Override
    public void addTab(String title, Component component) {

        this.addTab(title, component, null);
    }

    public void addTab(String title, Component component, Icon extraIcon) {

        super.addTab(title, new CloseTabIcon(extraIcon), component);
    }

    public void mouseClicked(MouseEvent e) {

        int tabNumber = getUI().tabForCoordinate(this, e.getX(), e.getY());
        if (tabNumber < 0) {
            return;
        }
        Rectangle rect = ((CloseTabIcon) getIconAt(tabNumber)).getBounds();
        if (rect.contains(e.getX(), e.getY())) {
            // the tab is being closed
            this.removeTabAt(tabNumber);
        }
    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    /**
     * The class which generates the 'X' icon for the tabs. The constructor accepts an icon which is extra to the 'X' icon, so you
     * can have tabs like in JBuilder. This value is null if no extra icon is required.
     */
    class CloseTabIcon implements Icon {

        private int xPos;
        private int yPos;
        private int width;
        private int height;
        private Icon fileIcon;

        public CloseTabIcon(Icon fileIcon) {

            this.fileIcon = fileIcon;
            this.width = 16;
            this.height = 16;
        }

        public void paintIcon(Component c, Graphics g, int x, int y) {

            this.xPos = x;
            this.yPos = y;

            Color col = g.getColor();

            g.setColor(Color.black);
            int yP = y + 2;
            g.drawLine(x + 1, yP, x + 12, yP);
            g.drawLine(x + 1, yP + 13, x + 12, yP + 13);
            g.drawLine(x, yP + 1, x, yP + 12);
            g.drawLine(x + 13, yP + 1, x + 13, yP + 12);
            g.drawLine(x + 3, yP + 3, x + 10, yP + 10);
            g.drawLine(x + 3, yP + 4, x + 9, yP + 10);
            g.drawLine(x + 4, yP + 3, x + 10, yP + 9);
            g.drawLine(x + 10, yP + 3, x + 3, yP + 10);
            g.drawLine(x + 10, yP + 4, x + 4, yP + 10);
            g.drawLine(x + 9, yP + 3, x + 3, yP + 9);
            g.setColor(col);
            if (fileIcon != null) {
                fileIcon.paintIcon(c, g, x + width, yP);
            }
        }

        public int getIconWidth() {

            return width + (fileIcon != null ? fileIcon.getIconWidth() : 0);
        }

        public int getIconHeight() {

            return height;
        }

        public Rectangle getBounds() {

            return new Rectangle(xPos, yPos, width, height);
        }

    }

}